package com.bfly.core.base.entity;

import com.bfly.common.json.JsonUtil;
import com.bfly.core.enums.SysError;

import java.io.Serializable;

/**
 * 统一响应数据格式包装类
 * @author 长沙商脉科技有限公司
 * @date 2020/9/16 15:56
 */
public class ResponseData implements Serializable {
    private static final long serialVersionUID = -3370071194314017595L;
    private int errno;
    private String errmsg;
    private Object data;
    private long timestamp;

    /**
     * 请求成功，返回未加密的JSON数据
     *
     * @author 长沙商脉科技有限公司
     * @date 2018/12/6 16:52
     */
    public static String getSuccess(Object msg) {
        ResponseData data = new ResponseData(200, msg);
        data.setErrno(200);
        data.setTimestamp(System.currentTimeMillis());
        return JsonUtil.toJsonFilter(data).toJSONString();
    }

    /**
     * 请求失败，返回未加密的JSON数据
     *
     * @author 长沙商脉科技有限公司
     * @date 2018/12/6 16:50
     */
    public static String getFail(SysError error, Object msg) {
        error = error == null ? SysError.ERROR : error;
        msg = msg == null ? error.getMessage() : msg;
        ResponseData data = new ResponseData(error.getCode(), "");
        data.setErrmsg(msg.toString());
        data.setErrno(error.getCode());
        data.setTimestamp(System.currentTimeMillis());
        return JsonUtil.toJsonFilter(data).toJSONString();
    }

    public ResponseData() {
    }

    public ResponseData(int code, Object msg) {
        this.errno = code;
        this.data = msg;
    }

    public int getErrno() {
        return errno;
    }

    public void setErrno(int errno) {
        this.errno = errno;
    }

    public String getErrmsg() {
        return errmsg;
    }

    public void setErrmsg(String errmsg) {
        this.errmsg = errmsg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }
}
